<template>
	<view class="invoice-auth-layout">
		<view class="invoice-auth-company">
			<text>签约主体</text>
			<text>{{jsonData.companyName}}</text>
		</view>
		<xh-input title="纳税类型" showTitle isSelect inputTextAlign="right" placeholder="请选择" @didSelect="showSelectType" v-model="selectType.name"></xh-input>
		<xh-input title="税率" showTitle isSelect inputTextAlign="right" placeholder="请选择" @didSelect="showSelectTaxRate" v-model="selectTaxRate.name"></xh-input>
		<view class="submit-btn" style="margin-top: 100rpx;" @tap="submit">提交</view>
		<uv-picker ref="pickerTypeRef" title="选择纳税类型" :columns="typeList" keyName="name" @confirm="selectTypeConfirm"></uv-picker>
		<uv-picker ref="pickerTaxRateRef" title="选择税率" :columns="taxRateList" keyName="name" @confirm="selectTaxRateConfirm"></uv-picker>
	</view>
</template>

<script setup>
	import { onLoad } from '@dcloudio/uni-app'
	import { ref } from 'vue'
	import { getSystemParams,invoiceAuth } from '@/common/api/api.js'
	
	const props = defineProps(['data'])
	const jsonData = JSON.parse(decodeURIComponent(props.data || '{}'))
	
	const typeList = ref([])
	const taxRateList = ref([])
	const pickerTypeRef = ref(null)
	const pickerTaxRateRef = ref(null)
	const selectType = ref({name:'',code:'0'})
	const selectTaxRate= ref({name:'',code:'0'})
	
	// 选择纳税类型
	const showSelectType = () => {
		pickerTypeRef.value.open()
	}
	// 选择税率
	const showSelectTaxRate = () => {
		pickerTaxRateRef.value.open()
	}
	// 选择纳税类型回调
	const selectTypeConfirm = (res)=>{
		selectType.value = res.value[0]
	}
	// 选择税率回调
	const selectTaxRateConfirm = (res)=>{
		selectTaxRate.value = res.value[0]
	}
	// 提交
	const submit = async () => {
		if(selectType.value.code == '0'){
			uni.$utils.showToast('请选择纳税身份')
			return
		}
		else if(selectTaxRate.value.code == '0'){
			uni.$utils.showToast('请选择纳税身份对应的税率')
			return
		}
		let params = {}
		params.payTaxType = selectType.value.code
		params.taxPoint = selectTaxRate.value.code
		if(jsonData.id){
			params.id = jsonData.id
		}
		await invoiceAuth(params)
		uni.$utils.showToast('开票认证提交成功')
		uni.navigateBack()
	}
	
	//获取纳税身份类型
	const getTypeData = async ()=>{
		const res = await getSystemParams({categoryCode:'TaxType'})
		typeList.value = [res.result]
	}
	//获取税率
	const getTaxRateData = async ()=>{
		const res = await getSystemParams({categoryCode:'taxrate'})
		taxRateList.value = [res.result]
	}
	onLoad(()=>{
		Promise.all([
			getTypeData(),
			getTaxRateData()
		])
	})
</script>

<style lang="scss" scoped>
	.invoice-auth-layout {
		padding-top: 20rpx;

		.invoice-auth-company {
			background-color: #fff;
			padding: 30rpx;
			display: flex;
			align-items: center;
			justify-content: space-between;
			margin-bottom: 20rpx;

			text:nth-child(1) {
				margin-right: 20rpx;
				flex-shrink: 0;
			}
		}
	}
</style>